import { getStyle, insertDom, pxToNumber, setStyle } from "./util.js"

class LineManager {
    constructor(data, container, option, deg, imageList, index) {
        this.container = container
        this.data = data
        this.option = option
        this.deg = deg
        this.imageList = imageList
        this.init(index)
    }

    init(index) {
        const direction = this.judegDirection()
        let canvasStyle = {
            left: '50%',
            transformOrigin: `left center`,
            transform: `translate(0,-50%) rotate(${this.deg}deg)`
        }
        if (direction === 'left') {
            canvasStyle = {
                left: '0%',
                transformOrigin: 'right center',
                transform: `translate(0,-50%) rotate(${this.deg - 180}deg)`
            }
        }
        const canvas = insertDom(this.container, { className: 'sc-line-item' }, { ...canvasStyle }, 'canvas')
        canvas.width = pxToNumber(getStyle(this.container, 'width')) / 2
        canvas.height = 150
        this.draw(canvas, direction, index)
    }

    judegDirection() {
        let direction = 'right'
        if (this.deg > 90 && this.deg < 270) {
            direction = 'left'
        }
        return direction
    }

    draw(canvas, direction, index) {
        const { image2GLeft, image2GRight, image5GLeft, image5GRight } = this.imageList
        const ctx = canvas.getContext('2d')
        ctx.beginPath()
        for (let i = 0; i <= canvas.width; i++) {
            if (i % (index % 2 === 0 ? 12 : 20) === 0) {
                let image = image2GLeft
                if (direction === 'right') {
                    image = image2GRight
                }
                if (index % 2 === 0) {
                    image = image5GLeft
                    if (direction === 'right') {
                        image = image5GRight
                    }
                }
                ctx.drawImage(image, i, (canvas.height - 16) / 2, 16, 16)
            }
        }
        ctx.closePath()

    }
}

export default LineManager